//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.mall.controller;

import com.example.mall.common.api.CommonPage;
import com.example.mall.common.api.CommonResult;
import com.example.mall.mbg.model.Indicator;
import com.example.mall.mbg.model.Role;
import com.example.mall.mbg.model.User;
import com.example.mall.service.IndicatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Api(tags = {"IndicatorController"}, description = "指标管理")
@Controller
@RequestMapping({"/indicator"})
public class IndicatorController {
    @Autowired
    private IndicatorService indicatorService;
    private static final Logger LOGGER = LoggerFactory.getLogger(IndicatorController.class);

    public IndicatorController() {
    }

    @ApiOperation("画面：指标库")
    @RequestMapping(value = {"init"},method = {RequestMethod.GET})
    public String init(Model model, HttpSession session) {
        List<Indicator> indicators = this.indicatorService.listAll();
        model.addAttribute("indicators", indicators);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "indicator-list";
    }

    @ApiOperation("获取所有指标列表")
    @RequestMapping(
            value = {"listAll"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<List<Indicator>> getIndicatorList() {
        return CommonResult.success(this.indicatorService.listAll());
    }

    @ApiOperation("添加指标")
    @RequestMapping(
            value = {"/create"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult createIndicator(@RequestBody Indicator indicator) {
        int count = this.indicatorService.create(indicator);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success(indicator);
            LOGGER.debug("createIndicator success:{}", indicator);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createIndicator failed:{}", indicator);
        }

        return commonResult;
    }

    @ApiOperation("画面：指标库编辑")
    @RequestMapping(value = {"edit"},method = {RequestMethod.GET})
    public String edit(Model model, Integer indicatorid, HttpSession session) {
        Indicator indicator = indicatorService.get(indicatorid);
        model.addAttribute("indicator", indicator);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "indicator-detail";
    }

    @ApiOperation("更新指定id指标信息")
    @RequestMapping(value = {"/update/{id}"}, method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult updateIndicator(@PathVariable("id") Integer indicatorid, @RequestBody Indicator indicator, BindingResult bindingResult) {
        indicator.setUpdatetime(new Date());
        int count = this.indicatorService.update(indicatorid, indicator);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success(indicator);
            LOGGER.debug("updateIndicator success:{}", indicator);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateIndicator failed:{}", indicator);
        }

        return commonResult;
    }

    @ApiOperation("删除指定id的指标")
    @RequestMapping(
            value = {"/delete/{id}"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult deleteIndicator(@PathVariable("id") Integer indicatorid) {
        int count = this.indicatorService.delete(indicatorid);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success((Object)null);
            LOGGER.debug("deleteIndicator success:id={}", indicatorid);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("deleteIndicator failed:id={}", indicatorid);
        }

        return commonResult;
    }

    @ApiOperation("分页查询指标列表")
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<CommonPage<Indicator>> listIndicator(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize) {
        List<Indicator> indicatorList = this.indicatorService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(indicatorList));
    }

    @ApiOperation("获取指定id的指标详情")
    @RequestMapping(
            value = {"/{id}"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<Indicator> indicator(@PathVariable("id") Integer indicatorid) {
        return CommonResult.success(this.indicatorService.get(indicatorid));
    }
}
